package com.mobileares.midp.widgets.client.pop;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.mobileares.midp.widgets.client.button.CButton;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-2
 * Time: 10:24:35
 * To change this template use File | Settings | File Templates.
 */
public class Message {
    public static final int _INFO = 1;
    public static final int _ERROR = 2;
    public static final int _ALERT = 3;
    public static final int _CONFIRM = 4;
    private int style;
    private Label message;
    private String mess;
    private IMessageConfirm confirmEvent;

    private PopPanel pop;


    public static Message INFO = new Message(_INFO, "");
    public static Message ERROR = new Message(_ERROR, "");
    public static Message ALERT = new Message(_ALERT, "");
    public static Message CONFIRM = new Message(_CONFIRM, "");

    public static void showInfo(String title, String mess) {
        showMe(title, mess, INFO);
    }

    public static void showError(String title, String mess) {
        showMe(title, mess, ERROR);
    }

    public static void showAlert(String title, String mess) {
        showMe(title, mess, ALERT);
    }

    public static void showConfirm(String title, String mess, IMessageConfirm call) {
        CONFIRM.setConfirmEvent(call);
        showMe(title, mess, CONFIRM);
    }

    private static void showMe(String title, String mess, Message ui) {
        ui.setWindowTitle(title);
        ui.setMessage(mess);
        ui.show();
    }


    public Message(int style, String mess) {
        this.style = style;
        this.mess = mess;
        pop = new PopPanel();
        initBodyWidget(pop.getPopWidget().getBodyWidget());
        initButtonWidget(pop.getPopWidget().getButtonWidget());
        pop.setWidth("320px");
    }

    public void setWindowTitle(String mess) {
        pop.getPopWidget().setHeadText(mess);
    }

    public void setMessage(String mess) {
        message.setText(mess);
    }

    public void setConfirmEvent(IMessageConfirm confirm) {
        this.confirmEvent = confirm;
    }

    public void show() {
        pop.show();
    }

    private void initBodyWidget(FlowPanel body) {
        String imagePath = "info.png";
        switch (style) {
            case _INFO:
                imagePath = "info.png";
                break;
            case _ERROR:
                imagePath = "error.png";
                break;
            case _ALERT:
                imagePath = "warning.png";
                break;
            case _CONFIRM:
                imagePath = "question.gif";

        }
        Image image = new Image("fishimages/pop/" + imagePath);
        image.setStyleName("icon");
        body.add(image);
        message = new Label(mess);
        message.setStyleName("message");
        body.add(message);
        body.setHeight("100px");
    }

    private void initButtonWidget(FlowPanel buttonPanel) {

        CButton confirm = new CButton("确定");
        buttonPanel.add(confirm);
        confirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                if (confirmEvent != null) {
                    confirmEvent.fire(true);
                }
                pop.hidden();
            }
        });
        if (style == _CONFIRM) {
            CButton cancel = new CButton("取消");
            buttonPanel.add(cancel);
            cancel.addClickHandler(new ClickHandler() {
                public void onClick(ClickEvent event) {
                    if (confirmEvent != null) {
                        confirmEvent.fire(false);
                    }
                    pop.hidden();
                }
            });
        }
    }
}
